package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbGroupActive;
import com.bwie.service.TbGroupActiveService;
import com.bwie.mapper.TbGroupActiveMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

/**
* @author 牛y
* @description 针对表【tb_group_active】的数据库操作Service实现
* @createDate 2025-07-26 11:37:16
*/
@Service
public class TbGroupActiveServiceImpl extends ServiceImpl<TbGroupActiveMapper, TbGroupActive>
    implements TbGroupActiveService{
    @Autowired
    TbGroupActiveMapper tbGroupActiveMapper;

    @Override
    public R getGroupActiveList(PageInfoVo pageInfoVo) {
        Page<TbGroupActive> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbGroupActive> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbGroupActive::getActiveName,pageInfoVo.getKeyWord());
        }
        wrapper.lambda().orderByDesc(TbGroupActive::getActiveId);
        Page<TbGroupActive> selectedPage = tbGroupActiveMapper.selectPage(page, wrapper);
        return R.OK(selectedPage);

    }

    @Override
    public R addGroupActive(TbGroupActive tbGroupActive, BindingResult result) {
        if(result.hasFieldErrors()){
            return R.ERROR(500,result.getFieldError().getDefaultMessage());
        }
        TbGroupActive active = tbGroupActiveMapper.selectOne(new QueryWrapper<TbGroupActive>().lambda().eq(TbGroupActive::getActiveName, tbGroupActive.getActiveName()));
        if(active!=null){
            return R.ERROR(500,"活动名称重复");
        }
        tbGroupActiveMapper.insert(tbGroupActive);
        return R.OK();
    }

    @Override
    public R updateGroupActive(TbGroupActive tbGroupActive) {
        TbGroupActive active = tbGroupActiveMapper.selectOne(new QueryWrapper<TbGroupActive>().lambda().eq(TbGroupActive::getActiveId, tbGroupActive.getActiveId()));
        if(active==null){
            return R.ERROR(500,"活动不存在");
        }
        tbGroupActiveMapper.updateById(tbGroupActive);
        return R.OK();
    }

    @Override
    public R deleteById(Integer activeId) {
        TbGroupActive active = tbGroupActiveMapper.selectById(activeId);
        if(active==null){
            return R.ERROR(500,"活动不存在");
        }
        tbGroupActiveMapper.deleteById(activeId);
        return R.OK();
    }
}




